package com.example.imagepro;

import com.example.imagepro.utils.CopyOfReadFile;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * 图像转换格式
 * ==改后缀名
 */
public class ImageConversion {
    public static void main(String[] args) {
        String url = "D:\\标注分类\\10.12";
        String airUrl = "D:\\图片标注";
        try {
            Process(url, airUrl);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @param url    源地址
     * @param airUrl 目标地址
     * @throws Exception
     */
    public static void Process(String url, String airUrl) throws Exception {
//         url = "E:\\picsave";
//         airUrl = "D:\\图片标注";
        List<String> readfile = CopyOfReadFile.readfile(url);
        int i = 0;
        for (String s : readfile) {
            BufferedImage bufferedImage;
            try {
                // 读取文件
                bufferedImage = ImageIO.read(new File(s));
                // 创建一个空白，RGB，相同的宽度和高度，和一个白色背景
                BufferedImage newBufferedImage = new BufferedImage(
                        bufferedImage.getWidth(), bufferedImage.getHeight(),
                        BufferedImage.TYPE_INT_RGB);
                // TYPE_INT_RGB:创建一个RBG图像，24位深度，成功将32位图转化成24位
                newBufferedImage.createGraphics().drawImage(bufferedImage, 0, 0,
                        Color.WHITE, null);
                // write to jpeg file
                long l = System.currentTimeMillis();
                ImageIO.write(newBufferedImage, "jpg", new File(airUrl + File.separator + l + ".jpg"));
                i++;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println("i:" + i);
    }

}
